-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rem_floor and rem_ceil #108193
base: master
Are you sure you want to change the base?
Add rem_floor and rem_ceil #108193
Conversation
r? @scottmcm (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting aside my lack of understanding on the use case, the implementation looks good to me.
What's a use case of this? |
I don't know…that's the point. But let's keep discussion of that in #88581. |
This comment has been minimized.
This comment has been minimized.
(FWIW I don't understand the docs error: I did define |
r? joshtriplett |
28d0fc4
to
d7600dc
Compare
(I decided to replace the link with a manual one so that it would pass the tests. No idea why it's failing otherwise.) |
The code looks fine here, assuming we're prepared to accept these from an API point of view. Given the questions expressed about use case, could you please file an ACP on these methods, specifically? (And since it came up in |
Pondering: would who wants a consistent div and rem result would pair That would look odd to me when reviewing, but it's also correct if Thus maybe it would be nice to have merged versions of these, so you get both the quotient and the remainder in one call, with a consistent rounding direction for both. |
I'd be more than happy to write up an ACP for this, although it's worth clarifying that I did include use cases in the original tracking issue: #88581 (comment) Ceiling remainder of unsigned integers is the one that I would consider weird and difficult to use, since it's asymmetrical: the remainder of ceiling division is always negative or zero, meaning that you have to return the absolute value of the result instead, or fail on non-exact division. It's incredibly important and worth repeating: integer division and remainder always come in pairs. Yes, there are a lot of cases where you just throw out the remainder because you're wanting to round something, but I feel like the terms "division" and "remainder" gives a lot of clarity into the sorts of operations that would use these for: you're dividing something into a series of pieces, and even if you often toss out the "remainder," there's still the opportunity to make use of it. While we often abstract division as part of other operations (like averaging), it's good to remember what it actually physically represents. |
d7600dc
to
5a8ef6e
Compare
This comment has been minimized.
This comment has been minimized.
5a8ef6e
to
7f6ac30
Compare
This comment has been minimized.
This comment has been minimized.
7f6ac30
to
0da93e6
Compare
This comment has been minimized.
This comment has been minimized.
@clarfonthey i think you would still need an ACP for this |
From previous experience with ACPs, these are only for new features, and adding onto existing features is mostly what tracking issues are for. I would be happy to write up an ACP for |
0da93e6
to
421a7b1
Compare
421a7b1
to
95a4369
Compare
95a4369
to
71a3fe8
Compare
/// | ||
/// ## Overflow behavior | ||
/// | ||
/// On overflow, this function will panic if overflow checks are enabled (default in debug |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed %
always panics on overflow (MIN / -1) regardless of the overflow checks compile option. At least this particular method will panic iff the %
inside it panics. Therefor, the documentation is incorrect. The already merged ceil and floor div functions should be double checked with this in mind.
Once that is fixed I suggest explaining exactly what constitutes an overflow. (The remainder function never truly overflows, but it panics whenever the corresponding division would also panic.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The already merged ceil and floor div functions should be double checked with this in mind.
Submitted a PR for that and some other stuff. #119726
r? libs-api |
71a3fe8
to
adf7d75
Compare
adf7d75
to
edc079b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I read most of the comments in #88581.
I am on board with signed rem_floor
and signed rem_ceil
, but not unsigned rem_floor
and unsigned unsigned_rem_ceil
. Would you like to remove those from this PR and I can merge it, or make one more attempt at synthesizing the rationale in favor of having the entire set?
Is there a particular reason you're against unsigned In terms of unsigned As I've said (and I sound like a broken record because I do genuinely believe this), a majority of cases that use division have a parallel case where the remainder could also be useful. For example, if you use Again, I agree that a lot of these fall into "but you could technically do it another way," but the primary goal of offering flooring and ceiling division was to make the intent of algorithms clearer, and I think offering remainders to all the division operators fits that, even if some are just identical to truncating division, and even if |
I am going to be proposing for unsigned |
I see. I'm not sure I like that approach since it does go against some of the other philosophies from the standard library-- for example, date algorithms are mentioned as a common case for |
☔ The latest upstream changes (presumably #127133) made this pull request unmergeable. Please resolve the merge conflicts. |
edc079b
to
08b59e8
Compare
Some changes occurred in src/tools/cargo cc @ehuss |
This comment has been minimized.
This comment has been minimized.
That error is… very weird. I get it locally too, but not when I switch to the master branch, despite… nothing related being changed? I'm hoping it's a weird error that gets fixed and I can rebase on a newer commit that fixes it. |
It looks like you updated all the submodules. You should amend your commit and remove those. |
08b59e8
to
c704b49
Compare
Huh, I had done a |
c704b49
to
1588c84
Compare
@clarfonthey this still requires an ACP as per #108193 (comment) unless a member has suggested it's fine without one. |
(FWIW, I haven't forgotten about that, and will begrudgingly file an ACP for it. I just haven't gotten around to it yet.) |
Since
rem_ceil
is awkward for unsigned integers, I've put it under a separate feature flag, although the result is still in the same tracking issue.I also removed the
rhs > 0
check on the unsigneddiv_ceil
since it's not necessary, and can be a bit confusing to readers. (The only alternative would berhs = 0
, which would panic before reaching this point.)cc #88581